package com.ryan.encrypttool.business.auth.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ryan.encrypttool.business.auth.dao.UserDao;
import com.ryan.encrypttool.business.auth.entity.input.UserSelectPageInput;
import com.ryan.encrypttool.business.auth.entity.po.User;
import com.ryan.encrypttool.business.auth.service.UserService;
import com.ryan.encrypttool.business.entity.vo.UserVO;
import com.ryan.encrypttool.common.entity.input.PageRequest;
import com.ryan.encrypttool.common.entity.vo.PageResultVO;

/**
 * 用户表(User)表服务实现类
 *
 * @author makejava
 * @since 2024-03-03 02:08:15
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Override
    public PageResultVO<UserVO> selectPage(PageRequest<UserSelectPageInput> pageRequest) {

        Page<User> page = new Page<>();

        page.setCurrent(pageRequest.getPageNo());
        page.setSize(pageRequest.getPageSize());
        Page<User> userPage = this.page(page, new QueryWrapper<>());
        List<User> records = userPage.getRecords();

        List<UserVO> result = new ArrayList<>();
        records.forEach(x -> {
            UserVO userVO = new UserVO();
            userVO.setId(x.getId());
            userVO.setName(x.getName());
            userVO.setMobile(x.getMobile());
            userVO.setUsername(x.getUsername());
            userVO.setOrgId(x.getOrgId());
            userVO.setUpdateTime(String.valueOf(x.getUpdateTime()));
            userVO.setCreateTime(String.valueOf(x.getCreateTime()));
            userVO.setRemark(x.getRemark());
            userVO.setState(x.getState());
            result.add(userVO);
        });

        PageResultVO<UserVO> resultVO = new PageResultVO<>();

        buildPageVO(resultVO, userPage, result);

        return resultVO;
    }

    @Override
    public List<UserVO> listAll(UserSelectPageInput userSelectPageInput) {
        QueryWrapper<User> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("name",userSelectPageInput.getName());
        queryWrapper.eq("id",userSelectPageInput.getId());

        List<User> userList = this.getBaseMapper().selectListByCondition(userSelectPageInput);

//        List<User> userList = this.list(queryWrapper);
        List<UserVO> result = new ArrayList<>();
        userList.forEach(x -> {
            UserVO userVO = new UserVO();
            userVO.setId(x.getId());
            userVO.setName(x.getName());
            userVO.setMobile(x.getMobile());
            userVO.setUsername(x.getUsername());
            userVO.setOrgId(x.getOrgId());
            userVO.setUpdateTime(String.valueOf(x.getUpdateTime()));
            userVO.setCreateTime(String.valueOf(x.getCreateTime()));
            userVO.setRemark(x.getRemark());
            userVO.setState(x.getState());
            result.add(userVO);
        });

        return result;
    }

    private static <T, E> void buildPageVO(PageResultVO<T> resultVO, Page<E> userPage, List<T> result) {
        resultVO.setTotal(userPage.getTotal());
        resultVO.setSize(userPage.getSize());
        resultVO.setCurrent(userPage.getCurrent());
        int hasNext = userPage.getTotal() - userPage.getCurrent() * userPage.getSize() > 0 ? 1 : 0;
        resultVO.setHasNext(hasNext);
        resultVO.setData(result);
    }
}
